package com.iotteam.androidbase.commen.views.charts;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.iotteam.androidbase.commen.utils.PLog;
import com.iotteam.androidbase.commen.views.charts.line.Data;
import com.iotteam.androidbase.commen.views.charts.line.Line;
import com.iotteam.androidbase.commen.views.charts.touch.TouchEventHandler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by vonchenchen on 14/08/2017.
 */

public class XYChartView extends View implements View.OnTouchListener{

    private Context mContext;

    private int mViewHeight;
    private int mViewWidth;

    private Axis mXAxis;
    private Axis mYAxis;

    private AxisRenderer mXRender;
    private AxisRenderer mYRender;

    private Paint mAxisPaint;

    private Point mLeftTopPoint;
    private Point mRightBottonPoint;

    private LineRenderer mLineRenderer;

    /** 线名字 线 */
    private Map<String, Line> mLineMap;

    private TouchEventHandler mTouchEventHandler;

    public XYChartView(Context context) {
        super(context);
        mContext = context;
        this.setOnTouchListener(this);
        init();
    }

    public XYChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        this.setOnTouchListener(this);
        initTouchEvent();
        init();
    }

    private void init(){
        if(mLineMap == null){
            mLineMap = new HashMap<>();
        }
        initTouchEvent();
    }

    private void initTouchEvent(){
        mTouchEventHandler = new TouchEventHandler();
    }

    private void initView(){

        //初始化轴线 刻度最大为24 最小为0 分为24份 水平放置
        mXAxis = new Axis(24, 0, 24, Axis.Oritation.Horizantal, mLeftTopPoint, mRightBottonPoint);
        mYAxis = new Axis(24, 0, 24, Axis.Oritation.Vertical, mLeftTopPoint, mRightBottonPoint);

        //初始化轴线画笔
        mAxisPaint = new Paint();
        mAxisPaint.setStyle(Paint.Style.FILL);
        mAxisPaint.setTextAlign(Paint.Align.CENTER);
        mAxisPaint.setColor(0xff000000);

        //初始化轴线渲染器
        mXRender = new AxisRenderer(mXAxis);
        mYRender = new AxisRenderer(mYAxis);

        mXRender.mHorizantalBaseLinePaint = mXRender.mVerticalBaseLinePaint = mXRender.mVerticalPaint = mXRender.mHorizantalPaint = mAxisPaint;
        mYRender.mHorizantalBaseLinePaint = mYRender.mVerticalBaseLinePaint = mYRender.mVerticalPaint = mYRender.mHorizantalPaint = mAxisPaint;
    }

    public void addLine(String lineName, Line line){

        mLineMap.put(lineName, line);
        invalidateView();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mViewHeight = getMeasuredHeight();
        mViewWidth = getMeasuredWidth();

        mLeftTopPoint = new Point(0, 0);
        mRightBottonPoint = new Point(mViewWidth, mViewHeight);

        initView();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mXRender.render(canvas);
        mYRender.render(canvas);

        for (Map.Entry<String, Line> entry : mLineMap.entrySet()) {
            Line line = entry.getValue();
            if(line.getRenderer() == null){
                //创建一个渲染器 并与 line绑定
                LineRenderer renderer = new LineRenderer(line, mXAxis, mYAxis);
            }
            line.getRenderer().render(canvas);
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {

        PLog.i("onTouch", "motionEvent "+motionEvent.getX()+" "+motionEvent.getY());

        return true;
    }

    private void invalidateView(){
        invalidate();
    }
}
